<?php
#图片上传

class UploadFileImage extends UploadFile {

	/**
	 * 上传
	 *
	 * @param array $file_infos		上传文件相关信息
	 * @param array 缩略尺寸
	 * @return unknown
	 */
	
	public function upload($file_infos, $sl_size='') {
		if (!is_array($file_infos['name'])) {
				$file_infos = array(
						'name' => array($file_infos['name']),
						'type' => array($file_infos['type']),					
						'tmp_name'=> array($file_infos['tmp_name']),
						'error' => array($file_infos['error']),
						'size' => array($file_infos['size']),
				);
			}
			$this->setOldFileInfo($file_infos);
			$this->setFileNum(count($file_infos['name']));
			$this->checkFileUpload();
		
			$full_filenames = array();
			for ($i=0; $i<$this->fileNum; $i++) {
				$file_infos['tmp_name'][$i] = $file_infos['tmp_name'][$i];
				$file_ext = $this->getFileExt($file_infos['name'][$i]);
				if ($this->isRename === false) {
					$full_filenames[] = $full_filename = $this->uploadPath . $file_infos['name'][$i];
				}else {
					$full_filenames[] = $full_filename = $this->uploadPath . date('YmdHis',time()) . mt_rand(1, 9).'.'.$file_ext;
				}
				
				if (!copy($file_infos['tmp_name'][$i], $full_filename)) {
					throw new uploadFileException('移动文件('.$file_infos['name'][$i].')失败!');
				}
	
				// 缩略尺寸不为空
				if (!empty($sl_size)) {
					if (!is_array($sl_size)) $sl_size = array($sl_size);
					$ext = pathinfo($file_infos['name'][$i], PATHINFO_EXTENSION);
					$file_info = pathinfo($full_filename);
					foreach ($sl_size as $size) {
						$oImage = createFromFile($file_infos['tmp_name'][$i], $ext);
						$s = explode('*', $size);
						$w = (int)$s[0];
						$h = (int)$s[1];
						$oImage->crop($w, $h, true, 1);
						$full_filename = $file_info['dirname'] .'/'. $file_info['filename'].'_'.$w.'_'.$h.'.'.$file_ext;
						$full_filenames[] = $full_filename;
						switch(strtolower($file_ext)){
							case 'jpg':
							case 'jpeg':
								$oImage->saveAsJpeg($full_filename);
								break;
							case 'png':
								$oImage->saveAsPng($full_filename);
								break;
							case 'gif':
								$oImage->saveAsGif($full_filename);
								break;
						}
						$oImage->destory();
						unset($oImage);
					}
				}
				@unlink($file_infos['tmp_name'][$i]);
			}
			return $full_filenames;
	}
}
